﻿using ajayumi.develop.win32.Transport.Utils;
using ajayumi.develop.win32.Transport.WCF.Services;
using ajayumi.develop.win32.Transport.WCF.Services.DataContracts;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;

namespace ajayumi.develop.win32.Transport.WCF.ServiceImpls
{
    /// <summary>
    /// UpgradeItem.cs
    /// Author   : 黄乙冬
    /// Date     : 2011-08-31
    /// </summary>
    public class UpgradeItem : UpgradeInfo, IComparable<UpgradeItem>
    {
        /// <summary>
        /// 差异更新版本目录路径
        /// </summary>
        public string DiffVerPath { get; set; }

        /// <summary>
        /// 完整更新版本目录路径
        /// </summary>
        public string FullVerPath { get; set; }


        public UpgradeItem()
        { }

        public UpgradeItem(XElement item)
        {
            this.LoadItem(item);
        }

        public UpgradeItem(UpgradeInfo item)
        {
            this.AppName = item.AppName;
            this.Version = item.Version;
            this.Description = item.Description;
            this.IsNeed = item.IsNeed;
            this.UpgradeRunMode = item.UpgradeRunMode;
            this.UpgradeMode = item.UpgradeMode;
            this.DiffUpgradeFiles = item.DiffUpgradeFiles;
            this.FullUpgradeFiles = item.FullUpgradeFiles;
            //this.DiffVerPath = item.DiffVerPath;
            //this.FullVerPath = item.FullVerPath;
        }

        private void LoadItem(XElement item)
        {
            XElement appNameElement = item.Element("AppName");
            this.AppName = appNameElement.Value;

            XElement versionElement = item.Element("Version");
            this.Version = versionElement.Value;

            XElement descriptionElement = item.Element("Description");
            this.Description = descriptionElement.Value;

            XElement isNeedElement = item.Element("IsNeed");
            if (isNeedElement.Value.Equals("1"))
            {
                this.IsNeed = true;
            }

            XElement runModeElement = item.Element("UpgradeRunMode");
            if (runModeElement != null && !string.IsNullOrEmpty(runModeElement.Value))
            { this.UpgradeRunMode = (UpgradeRunMode)Enum.Parse(typeof(UpgradeRunMode), runModeElement.Value); }
            else
            { this.UpgradeRunMode = Services.UpgradeRunMode.Normal; }

            XElement modeElement = item.Element("UpgradeMode");
            if (modeElement != null && !string.IsNullOrEmpty(modeElement.Value))
            { this.UpgradeMode = (UpgradeMode)Enum.Parse(typeof(UpgradeMode), modeElement.Value); }
            else
            { this.UpgradeMode = Services.UpgradeMode.Diff; }

            XElement upgradeFiles = item.Element("UpgradeFiles");
            this.DiffVerPath = upgradeFiles.Attribute("DiffVerPath").Value;
            this.FullVerPath = upgradeFiles.Attribute("FullVerPath").Value;

            string[] filePaths = null;
            List<FilePathInfo> lst = null;

            lst = new List<FilePathInfo>();
            if (!string.IsNullOrEmpty(this.DiffVerPath) && Directory.Exists(this.DiffVerPath))
            {
                filePaths = Directory.GetFiles(this.DiffVerPath, "*.*", SearchOption.AllDirectories);
                foreach (string path in filePaths)
                {
                    lst.Add(new FilePathInfo()
                    {
                        RootPath = this.DiffVerPath,
                        RelativePath = path.Replace(this.DiffVerPath, string.Empty).TrimStart('\\')
                    });
                }
            }
            this.DiffUpgradeFiles = lst.ToArray();

            lst = new List<FilePathInfo>();
            if (!string.IsNullOrEmpty(this.FullVerPath) && Directory.Exists(this.FullVerPath))
            {
                filePaths = Directory.GetFiles(this.FullVerPath, "*.*", SearchOption.AllDirectories);
                foreach (string path in filePaths)
                {
                    lst.Add(new FilePathInfo()
                    {
                        RootPath = this.FullVerPath,
                        RelativePath = path.Replace(this.FullVerPath, string.Empty).TrimStart('\\')
                    });
                }
            }
            this.FullUpgradeFiles = lst.ToArray();

            switch (this.UpgradeMode)
            {
                case UpgradeMode.Diff:
                    this.FullVerPath = this.DiffVerPath;
                    this.FullUpgradeFiles = this.DiffUpgradeFiles;
                    break;
                case UpgradeMode.Full:
                    this.DiffVerPath = this.FullVerPath;
                    this.DiffUpgradeFiles = this.FullUpgradeFiles;
                    break;
                default:
                    break;
            }
        }

        public int CompareTo(UpgradeItem other)
        {
            int version = VersionFormatter.ToInt(this.Version);
            int versionOther = VersionFormatter.ToInt(other.Version);
            return version.CompareTo(versionOther);
        }

        public UpgradeInfo ToUpgradeInfo()
        {
            return new UpgradeInfo()
            {
                AppName = this.AppName,
                Version = this.Version,
                Description = this.Description,
                UpgradeRunMode = this.UpgradeRunMode,
                //DiffVerPath = this.DiffVerPath,
                DiffUpgradeFiles = this.DiffUpgradeFiles,
                //FullVerPath = this.FullVerPath,
                FullUpgradeFiles = this.FullUpgradeFiles

            };
        }
    }
}
